package com.xyy.saas.payment;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.xyy.elasticjob.client.annotation.EnableElasticJob;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


/**
 * @author wangtianqi
 */
@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class, DataSourceAutoConfiguration.class})
@ComponentScan(basePackages={"com.xyy.*", "com.alibaba.druid.spring.boot.*", "com.xyy.apm"})
@EnableApolloConfig
@EnableElasticJob
@EnableDubbo
@EnableRetry
public class Start {
	public static void main(String[] args) {

		// 3.2 添加下面三个参数配置
		//spring容器关闭时，如果没有检测到dubbo销毁线程启动，就等待一段时间
		System.setProperty("dubbo.start.notiry.waiting.millis","5000"); //默认3秒，启动时等待通知客服端时间
		//dubbo销毁线程结束后，等待一段时间，再继续执行spring容器的销毁
		System.setProperty("dubbo.shutownhook.notiry.waiting.millis","5000"); //默认1秒，停用时等待通知客服端时间
		System.setProperty("dubbo.service.shutdown.wait","15000"); //默认10秒，dubbo优雅停机最大停机时间，设置优雅停机超时时间，如果超时则强制关闭。

		System.setProperty("cat.appName","xyy-saas-payment");
		SpringApplication app=new SpringApplication(Start.class);
		app.run(args);

	}

	private CorsConfiguration buildConfig() {
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		corsConfiguration.addAllowedOrigin("*");
		corsConfiguration.addAllowedHeader("*");
		corsConfiguration.addAllowedMethod("*");
		return corsConfiguration;
	}

	/**
	 * 跨域过滤器
	 * @return
	 */
	@Bean
	public CorsFilter corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", buildConfig());
		return new CorsFilter(source);
	}

}
